package com.kykj.kykjyzw.controller;

import com.kykj.kykjyzw.entity.ResultEntity;
import com.kykj.kykjyzw.entity.UserEntity;
import com.kykj.kykjyzw.service.impl.UserServiceImpl;
import com.kykj.kykjyzw.service.util.TokenToolService;
import com.kykj.kykjyzw.vo.UserVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("v2/user/")
@RestController
@CrossOrigin //解决跨域问题
public class UserController {
    @Resource
    UserServiceImpl userService;

    @Resource
    TokenToolService tokenToolService;
    @GetMapping("getuserlist")
    private ResultEntity getUserList(){
        ResultEntity resultEntity = new ResultEntity();
        try{
            resultEntity.setData(userService.getAllUser());
            resultEntity.setCode(0);
            resultEntity.setMsg("查询成功");
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("查询失败");
        }
        return resultEntity;
    }
    @PostMapping("user")
    private ResultEntity addUser(@RequestBody UserVo userVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if (userService.addUser(userVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("添加成功");
            }else {
                resultEntity.setCode(0);
                resultEntity.setMsg("添加失败");
        }}catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }
    @PutMapping ("user")
    private ResultEntity updateUser(@RequestBody UserVo userVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if (userService.updateUser(userVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("修改成功");
            }
            else {
                resultEntity.setCode(0);
                resultEntity.setMsg("修改失败");
            }
        }
        catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }
    @DeleteMapping ("user/{uid}")
    private ResultEntity delUser(@PathVariable("uid") int uid){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if (uid==1){//uid为1表示管理员，不允许删除
                resultEntity.setCode(2);
                resultEntity.setMsg("管理员不允许删除");
            }else {
                if (userService.delUser(uid)>0){
                    resultEntity.setCode(0);
                    resultEntity.setMsg("删除成功");
                }
                else {
                    resultEntity.setCode(0);
                    resultEntity.setMsg("删除失败");
                }
            }
        }
        catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }
    @GetMapping("user")
    private  ResultEntity getUserByPage(
            @RequestParam("uname") String uname,
            @RequestParam("page") int page,
            @RequestParam("limit") int limit){

        ResultEntity resultEntity=new ResultEntity();
        try{
            int start=(page-1)*limit;//计算起始位置
            int cnt=-1;
            List<UserEntity> list=userService.getUserByPage(uname,limit , start);
            if (uname.equals("")){
                cnt=userService.getUserCount();
            }else{
                cnt=list.size();
            }
            resultEntity.setCode(0);
            resultEntity.setData(list);
            resultEntity.setCount(cnt);
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误!");
        }return resultEntity;
    }
    @PostMapping("login")
    private ResultEntity dologin(
            @RequestBody UserVo userVo
    ){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if (userService.doLogin(userVo.getUname(),userVo.getUpwd())!=null){//登录成功
                resultEntity.setCode(0);
                resultEntity.setMsg("登录成功！");
                resultEntity.setData(tokenToolService.createToken(userVo.getUname()));//将token返回
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("用户名或者密码错误!");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误！");
        }

        return resultEntity;
    }
    /**
     * 退出登录
     * @return ResultEntity
     */
    @GetMapping("exitlogin")
    private ResultEntity exitLogin(@RequestParam("uname")String uname){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if (userService.exitLogin(uname)){
                resultEntity.setCode(0);
                resultEntity.setMsg("退出成功！");
            }else {
                resultEntity.setCode(1);
                resultEntity.setMsg("退出失败!");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("退出登录失败！");
        }
        return resultEntity;
    }
}

